<!DOCTYPE html>
<html>
<!--
Copyright 2009 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by an Apache 2.0 License.
See the COPYING file for details.
-->
<head>
<title>Closure Unit Tests - goog.events.actionEventWrapper</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.events');
  goog.require('goog.events.EventHandler');
  goog.require('goog.events.actionEventWrapper');
  goog.require('goog.testing.events');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<div id="a" tabindex="0"></div>
<script>

  var a = document.getElementById('a');
  var eh, events;

  function setUp() {
    events = [];
    eh = new goog.events.EventHandler();
  }


  function tearDown() {
    eh.dispose();
  }

  var Foo = function() {};
  Foo.prototype.test = function(e) {
    events.push(e);
  };

  function testAddActionListener() {
    assertEquals('No listeners registered yet', 0,
        goog.object.getCount(goog.events.listeners_));

    var listener = function(e) { events.push(e); }
    goog.events.listenWithWrapper(a, goog.events.actionEventWrapper, listener);

    assertEquals('2 listeners should be registered', 2,
        goog.object.getCount(goog.events.listeners_));

    goog.testing.events.fireClickSequence(a);
    assertEquals('1 event should have been dispatched', 1, events.length);
    assertEquals('Should be an click event', 'click', events[0].type)

    goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ENTER);
    assertEquals('2 events should have been dispatched', 2, events.length);
    assertEquals('Should be an keypress event', 'keypress', events[1].type)

    goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.SPACE);
    assertEquals('2 events should have been dispatched', 2, events.length);

    goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ESC);
    assertEquals('2 events should have been dispatched', 2, events.length);

    goog.events.unlistenWithWrapper(a, goog.events.actionEventWrapper,
        listener);
    assertEquals('no listeners should be registered', 0,
        goog.object.getCount(goog.events.listeners_));
  }


  function testRemoveActionListener() {
    assertEquals('No listeners registered yet', 0,
        goog.object.getCount(goog.events.listeners_));

    var listener1 = function(e) { events.push(e); };
    var listener2 = function(e) { events.push({type: 'err'}); }

    goog.events.listenWithWrapper(a, goog.events.actionEventWrapper, listener1);
    assertEquals('2 listeners should be registered', 2,
        goog.object.getCount(goog.events.listeners_));

    goog.events.listenWithWrapper(a, goog.events.actionEventWrapper, listener2);
    assertEquals('4 listeners should be registered', 4,
        goog.object.getCount(goog.events.listeners_));

    goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ENTER);
    assertEquals('2 events should have been dispatched', 2, events.length);
    assertEquals('Should be an keypress event', 'keypress', events[0].type);
    assertEquals('Should be an err event', 'err', events[1].type);

    goog.events.unlistenWithWrapper(a, goog.events.actionEventWrapper,
        listener2);
    assertEquals('2 listeners should be registered', 2,
        goog.object.getCount(goog.events.listeners_));

    events = [];
    goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ENTER);
    assertEquals('1 event should have been dispatched', 1, events.length);
    assertEquals('Should be an keypress event', 'keypress', events[0].type);

    goog.events.unlistenWithWrapper(a, goog.events.actionEventWrapper,
        listener1);
    assertEquals('0 listeners should be registered', 0,
        goog.object.getCount(goog.events.listeners_));
  }


  function testEventHandlerActionListener() {
    assertEquals('No listeners registered yet', 0,
        goog.object.getCount(goog.events.listeners_));

    var listener = function(e) { events.push(e); }
    eh.listenWithWrapper(a, goog.events.actionEventWrapper, listener);

    assertEquals('2 listeners should be registered', 2,
        goog.object.getCount(goog.events.listeners_));

    goog.testing.events.fireClickSequence(a);
    assertEquals('1 event should have been dispatched', 1, events.length);
    assertEquals('Should be an click event', 'click', events[0].type)

    goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ENTER);
    assertEquals('2 events should have been dispatched', 2, events.length);
    assertEquals('Should be an keypress event', 'keypress', events[1].type)

    goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.SPACE);
    assertEquals('2 events should have been dispatched', 2, events.length);

    goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ESC);
    assertEquals('2 events should have been dispatched', 2, events.length);

    eh.unlistenWithWrapper(a, goog.events.actionEventWrapper,
        listener);
    assertEquals('no listeners should be registered', 0,
        goog.object.getCount(goog.events.listeners_));
  }


  function testEventHandlerActionListenerWithScope() {
    assertEquals('No listeners registered yet', 0,
        goog.object.getCount(goog.events.listeners_));

    var foo = new Foo();
    var eh2 = new goog.events.EventHandler(foo);

    eh2.listenWithWrapper(a, goog.events.actionEventWrapper, foo.test);

    assertEquals('2 listeners should be registered', 2,
        goog.object.getCount(goog.events.listeners_));

    goog.testing.events.fireClickSequence(a);
    assertEquals('1 event should have been dispatched', 1, events.length);
    assertEquals('Should be an click event', 'click', events[0].type)

    goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ENTER);
    assertEquals('2 events should have been dispatched', 2, events.length);
    assertEquals('Should be an keypress event', 'keypress', events[1].type)

    goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.SPACE);
    assertEquals('2 events should have been dispatched', 2, events.length);

    goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ESC);
    assertEquals('2 events should have been dispatched', 2, events.length);

    eh2.dispose();
    assertEquals('no listeners should be registered', 0,
        goog.object.getCount(goog.events.listeners_));
    delete foo;
  }

</script>
</body>
</html>
